/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.jiujiudevice.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.boxin.ims.modules.jiujiudevice.entity.JiuConsumeConfig;
import com.boxin.ims.modules.jiujiudevice.service.JiuConsumeConfigService;

/**
 * 消费配置Controller
 * @author sxh
 * @version 2018-11-14
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/jiujiudevice/jiuConsumeConfig")
public class JiuConsumeConfigController extends BaseController {

	@Autowired
	private JiuConsumeConfigService jiuConsumeConfigService;
	
	@ModelAttribute
	public JiuConsumeConfig get(@RequestParam(required=false) Long id) {
		if (id != null){
			return jiuConsumeConfigService.get(id);
		}else{
			return new JiuConsumeConfig();
		}
	}
	
	@RequiresPermissions("jiujiudevice:jiuConsumeConfig:view")
	@RequestMapping(value = {"list", ""})
	public String list(JiuConsumeConfig jiuConsumeConfig, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		if (!user.isAdmin()){
			jiuConsumeConfig.setUser(user);
		}
        Page<JiuConsumeConfig> page = jiuConsumeConfigService.find(new Page<JiuConsumeConfig>(request, response), jiuConsumeConfig); 
        model.addAttribute("page", page);
		return "modules/jiujiudevice/jiuConsumeConfigList";
	}

	@RequiresPermissions("jiujiudevice:jiuConsumeConfig:view")
	@RequestMapping(value = "form")
	public String form(JiuConsumeConfig jiuConsumeConfig, Model model) {
	
		User user = UserUtils.getUser();
		if (jiuConsumeConfig.getUser() == null || jiuConsumeConfig.getUser().getId() <1){
			jiuConsumeConfig.setUser(user);
		}
		
		model.addAttribute("jiuConsumeConfig", jiuConsumeConfig);
		return "modules/jiujiudevice/jiuConsumeConfigForm";
	}

	@RequiresPermissions("jiujiudevice:jiuConsumeConfig:edit")
	@RequestMapping(value = "save")
	public String save(JiuConsumeConfig jiuConsumeConfig, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, jiuConsumeConfig)){
			return form(jiuConsumeConfig, model);
		}
		jiuConsumeConfigService.save(jiuConsumeConfig);
		addMessage(redirectAttributes, "保存消费配置'" + jiuConsumeConfig.getName() + "'成功");
		return "redirect:"+Global.ADMIN_PATH+"/jiujiudevice/jiuConsumeConfig/?repage";
	}
	
	@RequiresPermissions("jiujiudevice:jiuConsumeConfig:edit")
	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		jiuConsumeConfigService.delete(id);
		addMessage(redirectAttributes, "删除消费配置成功");
		return "redirect:"+Global.ADMIN_PATH+"/jiujiudevice/jiuConsumeConfig/?repage";
	}

}
